# coding:utf-8


import os.path as op
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileRequired
import flask_admin.form as form
from wtforms import Field, StringField, SubmitField, DateField, DateTimeField, TextAreaField, FileField
from wtforms.widgets import TextInput
from wtforms.validators import DataRequired, optional

from config import basedir


class TagListField(Field):
    widget = TextInput()

    def _value(self):
        if self.data:
            return u','.join(self.data)
        else:
            return u''

    def process_formdata(self, valuelist):
        if valuelist:
            self.data = [x.strip() for x in valuelist[0].split(',')]
        else:
            self.data = []


class MaterialForm(FlaskForm):
    name = StringField(u'物品的名称?', validators=[DataRequired()])
    keywords = TagListField(u'关键字?', validators=[DataRequired()])
    storage_time = DateTimeField(u'存放时间?', format='%Y-%m-%d %H:%M:%S', validators=[DataRequired()])
    location = StringField(u'物品的位置?', validators=[DataRequired()])
    picture = form.ImageUploadField(u'物品的图片？', base_path=op.join(basedir, 'app', 'photos'),
                                    thumbnail_size=(100, 100, True),
                                    endpoint='images')
    description = TextAreaField(u'物品的描述?', validators=[optional()])
    expiration = DateField(u'过期时间?', format='%Y-%m-%d', validators=[optional()])
    submit = SubmitField(u'增加')


class UploadForm(FlaskForm):
    batch_file = FileField(u'批量增加所用配置文件？', validators=[FileRequired(), FileAllowed(['txt', ], u'只能是txt文件!')])
    submit = SubmitField(u'增加')